//
//  JinTabbarController.swift
//  微博项目
//
//  Created by admin on 16/3/1.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class JinTabbarController: UITabBarController,JinTabBarDelegate {

    override class func initialize()
    {
//        appearance.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Normal)
    }
    let tabBars = Tabbar.instance()
    func getMessage()
    {
        Message.getMessage { (message) -> (Void) in
            (self.myTabBar?.viewWithTag(1) as! TabbarButton).badge.badge = Int(message.status)
            (self.myTabBar?.viewWithTag(2) as! TabbarButton).badge.badge = Int(message.group) + Int(message.private_group)
            (self.myTabBar?.viewWithTag(3) as! TabbarButton).badge.badge = Int(message.mention_cmt) + Int(message.mention_status)
            (self.myTabBar?.viewWithTag(4) as! TabbarButton).badge.badge = Int(message.notice) + Int(message.invite)
            // 图标上的数字
            UIApplication.sharedApplication().applicationIconBadgeNumber = Int(message.invite) + Int(message.notice) + Int(message.mention_status) + Int(message.mention_cmt) + Int(message.group) + Int(message.mention_cmt) + Int(message.group) + Int(message.group) + Int(message.status)
        }
    }
    func setAllTabBarItem()
    {
        let home = HomeController()
        self.setTabbarItem(home,title: tabBars[0].title,image: tabBars[0].image,selectedImage: tabBars[0].selectedImage)
        let message = MessageController()
        self.setTabbarItem(message,title: tabBars[1].title,image: tabBars[1].image,selectedImage: tabBars[1].selectedImage)
        let discover = DiscoverController()
        self.setTabbarItem(discover,title: tabBars[2].title,image: tabBars[2].image,selectedImage: tabBars[2].selectedImage)
        let profile = ProfileController()
        self.setTabbarItem(profile,title: tabBars[3].title,image: tabBars[3].image,selectedImage: tabBars[3].selectedImage)
    }
    func setTabbarItem(controller:UIViewController,title:String,image:String,selectedImage:String)
    {
        // homeVC.view = UIView(frame: window.frame)
        let navigationController = JInNavgationController(rootViewController: controller)
        controller.title = title
        controller.view.backgroundColor = UIColor.whiteColor()
        controller.tabBarItem.title = title
        var image = UIImage(imageLiteral: image)
        image = image.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        controller.tabBarItem.image = image
        image = UIImage(imageLiteral: selectedImage)
        controller.tabBarItem.selectedImage = image.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        self.addChildViewController(navigationController)
    }
    var getMessageTimer:NSTimer?
    weak var myTabBar:JinTabBar?
    override func viewDidLoad() {
        super.viewDidLoad()
        let mTabBar = JinTabBar()
        mTabBar.addButtons(tabBars)
        mTabBar.tabbarButtonDelegate = self
        mTabBar.frame = self.tabBar.bounds
//        self.view.addSubview(tabBar)
        self.tabBar.addSubview(mTabBar)
        self.myTabBar = mTabBar
//        self.tabBar.removeFromSuperview()
//        self.setValue(tabBar, forKey: "tabBar")
        
        //初始化定时器
        self.getMessageTimer = NSTimer(timeInterval: 3, target: self, selector: "getMessage", userInfo: nil, repeats: true)
        //获得当前消息循环
        var runLoop = NSRunLoop.currentRunLoop()
        //把定时器以默认模式添加进当前消息循环，分为两种模式
        //        runLoop.addTimer(self.timer, forMode: NSDefaultRunLoopMode)
        runLoop.addTimer(self.getMessageTimer!, forMode: NSRunLoopCommonModes)
    }
    override func viewDidAppear(animated: Bool) {
        for view in self.tabBar.subviews
        {
            if view.isKindOfClass(NSClassFromString("UITabBarButton")!)
            {
                view.removeFromSuperview()
            }
        }
    }
    func tabbarButtonClick(index: Int) {
        self.selectedIndex = index
    }
    func sendStatusButtonClick()
    {
        let sendStatusController = SendStatusController()
        let navigationController = JInNavgationController(rootViewController: sendStatusController)
        self.presentViewController(navigationController, animated: true, completion: nil)
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
